package cct.dialogs;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import cct.interfaces.MoleculeRendererInterface;

/**
 * <p>Title: Jamberoo - Computational Chemistry Toolkit</p>
 *
 * <p>Description: Collection of Computational Chemistry related code</p>
 *
 * <p>Copyright: Copyright (c) 2005-2010 Dr. Vladislav Vasilyev</p>
 *
 * <p>Company: The Australian National University</p>
 *
 * @author Dr. Vladislav Vasilyev
 * @version 1.0
 */
public class AddMoleculeDialog
    extends JDialog {

  private JPanel panel1 = new JPanel();
  private BorderLayout borderLayout1 = new BorderLayout();
  private JPanel jPanel1 = new JPanel();
  private JPanel jPanel2 = new JPanel();
  private JButton cancelButton = new JButton();
  private JButton jButton2 = new JButton();
  private JRadioButton overwriteRadioButton = new JRadioButton();
  private BorderLayout borderLayout2 = new BorderLayout();
  private JRadioButton newWinRadioButton = new JRadioButton();
  private JRadioButton mergeRadioButton = new JRadioButton();
  private ButtonGroup buttonGroup1 = new ButtonGroup();

  private int cond = 1;

  public AddMoleculeDialog(Frame owner, String title) {
    super(owner, title, true);
    try {
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      jbInit();
      pack();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

  public AddMoleculeDialog(Dialog owner, String title) {
    super(owner, title, true);
    try {
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      jbInit();
      pack();
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

  public int getLoadMode() {
    if (overwriteRadioButton.isSelected()) {
      return MoleculeRendererInterface.OVERWRITE_MOLECULE;
    }
    else if (newWinRadioButton.isSelected()) {
      return MoleculeRendererInterface.NEW_MOLECULE;
    }

    return MoleculeRendererInterface.MERGE_MOLECULE;
  }

  public void EnableNewWindowButton(boolean enable) {
    newWinRadioButton.setVisible(enable);
    this.validateTree();
  }

  public AddMoleculeDialog() {
    this(new Frame(), "AddMoleculeDialog");
  }

  private void jbInit() throws Exception {
    panel1.setLayout(borderLayout1);
    cancelButton.setToolTipText("");
    cancelButton.setText("Cancel");
    cancelButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        cancelButton_actionPerformed(e);
      }
    });
    jButton2.setToolTipText("");
    jButton2.setText(" Open ");
    jButton2.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        jButton2_actionPerformed(e);
      }
    });
    overwriteRadioButton.setToolTipText("Erase Molecule in Active Window and Load a New One");
    overwriteRadioButton.setSelected(true);
    overwriteRadioButton.setText("Overwrite Molecule in Active Window");
    jPanel1.setLayout(borderLayout2);
    newWinRadioButton.setToolTipText("Load Molecule into New Window");
    newWinRadioButton.setText("Open in New Window");
    mergeRadioButton.setToolTipText("Append New Molecule to a Molecule in Active Window");
    mergeRadioButton.setText("Merge with Molecule in Active Window");
    getContentPane().add(panel1);
    panel1.add(jPanel1, java.awt.BorderLayout.CENTER);
    panel1.add(jPanel2, java.awt.BorderLayout.SOUTH);
    jPanel2.add(jButton2);
    jPanel2.add(cancelButton);
    jPanel1.add(newWinRadioButton, java.awt.BorderLayout.SOUTH);
    buttonGroup1.add(overwriteRadioButton);
    jPanel1.add(mergeRadioButton, java.awt.BorderLayout.CENTER);
    jPanel1.add(overwriteRadioButton, java.awt.BorderLayout.NORTH);
    buttonGroup1.add(newWinRadioButton);
    buttonGroup1.add(mergeRadioButton);
  }

  public int showDialog() {
    setVisible(true);
    return cond;
  }

  public void jButton2_actionPerformed(ActionEvent e) {
    cond = getLoadMode();
    setVisible(false);
  }

  public void cancelButton_actionPerformed(ActionEvent e) {
    cond = MoleculeRendererInterface.LOAD_CANCELED;
    setVisible(false);
  }
}
